package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Override
    public void add(List<OrderSetting> orderSettingList) {
        if(orderSettingList != null && orderSettingList.size() > 0 ) {
            //遍历集合
            for (OrderSetting orderSetting : orderSettingList) {
                //从后台获取数据，进行比较
                long count = orderSettingDao.findByDate(orderSetting.getOrderDate());

                if(count > 0) {
                    //进行预约数据更新
                    orderSettingDao.edit(orderSetting);
                } else {
                    //进行添加
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }

    @Override
    public List<Map<String,Object>> findBydate(String date) {
        List<OrderSetting> orderSettingList = orderSettingDao.findBydate(date);

        List<Map<String,Object>> ordersettingMap = new ArrayList<>();

        if(orderSettingList != null && orderSettingList.size() > 0) {
            for (OrderSetting orderSetting : orderSettingList) {
                Map<String,Object> map = new HashMap<>();
                map.put("date",orderSetting.getOrderDate().getDate());
                map.put("number",orderSetting.getNumber());
                map.put("reservations",orderSetting.getReservations());
                ordersettingMap.add(map);
            }
        }
        return ordersettingMap;
    }

    @Override
    public void editnumberBydate(OrderSetting orderSetting) {
        long count = orderSettingDao.findByDate(orderSetting.getOrderDate());

        if(count > 0) {
            //进行预约数据更新
            orderSettingDao.edit(orderSetting);
        } else {
            //进行添加
            orderSettingDao.add(orderSetting);
        }
    }
}
